package com.neu.questionnaire_system.service.impl;

import com.neu.questionnaire_system.beans.ResponseEntity;
import com.neu.questionnaire_system.entity.QuestionHistory;
import com.neu.questionnaire_system.mapper.QuestionHistoryMapper;
import com.neu.questionnaire_system.mapper.QuestionnaireMapper;
import com.neu.questionnaire_system.service.QuestionHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neu.questionnaire_system.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tkj
 * @since 2022-11-13
 */
@Service
public class QuestionHistoryServiceImpl extends ServiceImpl<QuestionHistoryMapper, QuestionHistory> implements QuestionHistoryService {

    @Autowired
    private QuestionHistoryMapper questionHistoryMapper;
    @Autowired
    private QuestionnaireServiceImpl questionnaireService;

    //批量插入问题记录
    public ResponseEntity saveBatchQuestionHistory(String qid,List<QuestionHistory> list){
        ResponseEntity response = new ResponseEntity();
        if (questionnaireService.updateCount(qid)>0){
            boolean result = this.saveBatch(list);
            response.setData(result);
            if(result){
                response.setCode(Constants.SUCCESS_CODE);
                response.setMessage(Constants.ADD_MESSAGE);
            }else {
                response.setCode(Constants.FAIL_CODE);
                response.setMessage(Constants.ADD_FAIL_MESSAGE);
            }
            return response;
        }else {
            response.setCode(Constants.FAIL_CODE);
            response.setMessage(Constants.ADD_FAIL_MESSAGE);
            return response;
        }
    }

    //根据问卷id获取全部问题作答记录,各个答案的作答人数
    public ResponseEntity selectAllHistoryCountByQid(String qid){
        ResponseEntity response = new ResponseEntity();
        List<Map<String, Object>> result = questionHistoryMapper.selectAllHistoryCountByQid(qid);
        response.setData(result);
        if(result!=null){
            response.setMessage(Constants.SELECT_MESSAGE);
            response.setCode(Constants.SUCCESS_CODE);
        }else{
            response.setMessage(Constants.SELECT_FAIL_MESSAGE);
            response.setCode(Constants.FAIL_CODE);
        }
        return response;
    }

    //根据问卷id获取全部问题作答记录，具体每题的每个答案作答人是谁
    public ResponseEntity selectAllHistoryByQid(String qid){
        ResponseEntity response = new ResponseEntity();
        List<Map<String, Object>> result = questionHistoryMapper.selectAllHistoryByQid(qid);
        response.setData(result);
        if(result!=null){
            response.setMessage(Constants.SELECT_MESSAGE);
            response.setCode(Constants.SUCCESS_CODE);
        }else{
            response.setMessage(Constants.SELECT_FAIL_MESSAGE);
            response.setCode(Constants.FAIL_CODE);
        }
        return response;
    }

}
